require 'sinatra/base'

# Namespaces Pyro::Linker.
module Pyro
  # Public: Linker helpers for Pyro::App.
  #
  # * Used by Pyro::Compiler.
  module Linker
    # Public: Formats the attrs argument into html tag attributes.
    #
    # Example
    #
    #   format_attrs
    #
    # Returns a String containing html tag attributes.
    def format_attrs
      attrs = ''
      if settings.args[:attrs]
        settings.args[:attrs].each do |attr|
          attrs += " #{attr.first}='#{attr.last}'"
        end
      end
      attrs
    end

    # Public: Generates an html tag linking to a target file.
    #
    # target - Path/file to link to.
    #
    # Example
    #
    #   generate_link(target)
    #
    # Returns a String containing an html tag.
    def generate_link(target)
      src   = "#{target.sub(settings.public_folder, '')}?#{settings.timestamp}"
      attrs = format_attrs()

      link = js_tag(src, attrs)  if File.extname(target) == '.js'
      link = css_tag(src, attrs) if File.extname(target) == '.css'

      settings.links.include?(link) ? link = '' : settings.links << link

      link
    end

    # Public: Tempalte for <script> tags.
    #
    # src   - The .js file to link to.
    # attrs - The tag attributes to use.
    #
    # Example
    #
    #   js_tag(src, attrs)
    #
    # Returns a String containing a <script> tag.
    def js_tag(src, attrs)
      "<script type='text/javascript' src='#{src}'#{attrs}> </script>\n"
    end

    # Public: Tempalte for <link> tags.
    #
    # src   - The .css file to link to.
    # attrs - The tag attributes to use.
    #
    # Example
    #
    #   css_tag(src, attrs)
    #
    # Returns a String containing a <link> tag.
    def css_tag(src, attrs)
      "<link type='text/css' rel='stylesheet' href='#{src}'#{attrs} />\n"
    end

    # Public: Figures out what to name handlebars templates.
    #
    # file - The path/file to name the template after if
    #        neither name or src arguments are set.
    #
    # Example
    #
    #   template_name(file)
    #
    # Returns a String containing the template name.
    def template_name(file)
      if settings.args[:name]
        settings.args[:name]
      elsif settings.args[:dir]
        template_name_from_dir(file)
      elsif settings.args[:src]
        template_name_from_src()
      end
    end

    # Public: Figures out what to name a handlebars template from a file/path.
    #
    # file - The path/file to name the template after.
    #
    # Example
    #
    #   template_name_from_dir(file)
    #
    # Returns a String containing the template name.
    def template_name_from_dir(file)
      file.sub("#{settings.root}/#{settings.args[:dir]}/", '')
          .sub("/#{file.split('/').last}", '')
    end

    # Public: Figures out what to name a handlebars template
    #         from the src argument.
    #
    # Example
    #
    #   template_name_from_src
    #
    # Returns a String containing the template name.
    def template_name_from_src
      settings.args[:src].sub(settings.root, '')
                         .sub(File.extname(settings.args[:src]), '')
    end
  end
end